/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller.compound;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.annotation.Inner;
import com.nai365.nart.operation.entity.CompoundDemand;
import com.nai365.nart.operation.service.CompoundDemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 合成需求
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/compound/demand")
@Api(value = "compound", tags = "藏品合成")
public class CompoundDemandController {

	private final CompoundDemandService compoundDemandService;


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param compoundDemand 合成需求
	 * @return R<Page < CompoundDemand>>
	 */
	@ApiOperation(value = "合成需求分页查询", tags = {"藏品合成-PC端"})
	@GetMapping("/page")
	public R<Page<CompoundDemand>> getCompoundDemandPage(Page<CompoundDemand> page, CompoundDemand compoundDemand) {
		LambdaQueryWrapper<CompoundDemand> queryWrapper = Wrappers.lambdaQuery();
		if (!StringUtils.isBlank(compoundDemand.getTitle())) {
			queryWrapper.like(CompoundDemand::getTitle, compoundDemand.getTitle());
		}
		if (compoundDemand.getIsPublished() != null) {
			queryWrapper.eq(CompoundDemand::getIsPublished, compoundDemand.getIsPublished());
		}
		if (compoundDemand.getTargetSpuId() != null) {
			queryWrapper.eq(CompoundDemand::getTargetSpuId, compoundDemand.getTargetSpuId());
		}
		return R.ok(compoundDemandService.page(page, queryWrapper));
	}


	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @return R<Page < CompoundDemand>>
	 */
	@ApiOperation(value = "已发布合成需求分页查询", tags = {"藏品合成-移动端"})
	@GetMapping("/live")
	@Inner(value = false)
	public R<Page<CompoundDemand>> live(@ApiParam("page") Page<CompoundDemand> page) {
		LambdaQueryWrapper<CompoundDemand> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundDemand::getIsPublished, true);
		return R.ok(compoundDemandService.page(page, queryWrapper));
	}


	/**
	 * 通过id查询合成需求
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "合成需求详情", tags = {"藏品合成-PC端", "藏品合成-移动端"})
	@GetMapping("/{id}")
	@Inner(value = false)
	public R<CompoundDemand> getById(@PathVariable("id") @ApiParam("合成需求id") Long id) {
		return R.ok(compoundDemandService.getById(id));
	}

	/**
	 * 通过id查询合成需求,结果包含我拥有数量
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "查询合成需求详情，包含我拥有可用的资源", tags = {"藏品合成-移动端"})
	@GetMapping("/{id}/owner")
	public R<CompoundDemand> getOwnerCountById(@PathVariable("id") Long id) {
		return R.ok(compoundDemandService.getAbleCountById(id));
	}


	/**
	 * 新增合成需求
	 *
	 * @param compoundDemand 合成需求
	 * @return R
	 */
	@ApiOperation(value = "新增合成需求", tags = {"藏品合成-PC端"})
	@SysLog("新增合成需求")
	@PostMapping
	public R<CompoundDemand> save(@RequestBody CompoundDemand compoundDemand) {
		if (compoundDemand.getResourcesList() != null && !compoundDemand.getResourcesList().isEmpty()) {
			this.compoundDemandService.save(compoundDemand);
			return R.ok(compoundDemand);
		} else {
			return R.failed("合成材料不能为空");
		}
	}

	/**
	 * 修改合成需求
	 *
	 * @param compoundDemand 合成需求
	 * @return R
	 */
	@ApiOperation(value = "修改合成需求", tags = {"藏品合成-PC端"})
	@SysLog("修改合成需求")
	@PutMapping
	public R<CompoundDemand> updateById(@RequestBody CompoundDemand compoundDemand) {
		if (compoundDemand.getResourcesList() != null && !compoundDemand.getResourcesList().isEmpty()) {
			this.compoundDemandService.updateById(compoundDemand);
			return R.ok(compoundDemand);
		} else {
			return R.failed("合成材料不能为空");
		}
	}

	/**
	 * 通过id删除合成需求
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "删除合成需求", tags = {"藏品合成-PC端"})
	@SysLog("通过id删除合成需求")
	@DeleteMapping("/{id}")
	public R<Boolean> removeById(@PathVariable @ApiParam("合成需求") Long id) {
		return R.ok(compoundDemandService.removeById(id));
	}


}
